### Replication Archive for "Child Welfare System Contact and Voting", Spring 2025
### This script pulls in values calculated in the Stata do files
### And plots them 

library(readxl)
library(ggplot2)
theme_set(theme_bw()) #drop the gray background

rm(list=ls())

#make main bar plots for paper and SI
race <- read_excel("DescriptiveBarGraphRace.xls") #output from Stata
race <- race[c(10,9,11:14, 5:8,4,3,1:2),] #reorder so younger is before older etc
race[race$Group=="Young", "Group"] <- "Under 26"
race[race$Group=="Older", "Group"] <- "Age 26+"
race[race$Group=="Referred in 2016 or after", "Group"] <- "Referred \n2016-on"
race[race$Group=="Referred prior to 2016", "Group"] <- "Referred \npre-2016"
race[race$Group=="Multiple Referrals", "Group"] <- "Two+ \n Referrals"

raceadd <- race[7:14,] #lop off the additional rows to use later
race <- race[1:6,] #keep original rows for main plot in paper

pdf("DescriptiveBarGraph_Race_inR.pdf", width=10, height=5) #main plot: figure 2 in paper

spacing <- c(0,.35,1.05,.35,1.05,.35)
barplot(race$Registered,names=race$Group, space=spacing, col="lightgray",main="Registration and Voting by Demographic Characteristics", ylab="Percent Registered/Voting in 2016", cex.main=1.3, cex.lab=1.05, cex.names=.88, mgp = c(2.5, 0.5, 0))
barplot(race$Voted,names=NULL, space=c(0.2,.35,1.05,.35,1.05,.35), col="gray30", mgp = c(2.5, 0.5, 0), add=T)
par(fig=c(0, 1, 0, 1), oma=c(0, 0, 0, 0), mar=c(0, 0, 0, 0), new=TRUE)
plot(0, 0, type='n', bty='n', xaxt='n', yaxt='n')
legend("bottom" , horiz=TRUE, inset=c(-0.1,0), legend=c("Registered", "Voted 2016"), fill=c("lightgray","gray30"))
dev.off()

#and now make a little supplemental plot for SI
pdf("DescriptiveBarGraph_OtherFeatures_inR.pdf", width=10, height=4) 
spacing <- c(0,.35,1.05,.35,1.05,.35,1.05,.35)
barplot(raceadd$Registered,names=raceadd$Group, space=spacing, col="lightgray",main="Registration and Voting by Case Characteristics", ylab="Percent Registered/Voting in 2016", cex.main=1.3, cex.lab=1.05, cex.names=.85, mgp = c(2.5, 0.5, 0))
barplot(raceadd$Voted,names=NULL, space=c(0.2,.35,1.05,.35,1.05,.35,1.05,.35), col="gray30", mgp = c(2.5, 0.5, 0), add=T)
par(fig=c(0, 1, 0, 1), oma=c(0, 0, 0, 0), mar=c(0, 0, 0, 0), new=TRUE)
plot(0, 0, type='n', bty='n', xaxt='n', yaxt='n')
legend("bottom" , horiz=TRUE, inset=c(-0.1,0), legend=c("Registered", "Voted 2016"), fill=c("lightgray","gray30"))
dev.off()

###Now,Figure 1 from the paper (bar graph by exposure to CPS)
cps <-  read_excel("Fig1data.xls") #output from Stata
cps <- cps[c(5,1:4),]#reorder so county comparison is first
colnames(cps)<- c("CPSmaxinter", "Registered", "Voted")


pdf("DescriptiveBarGraph_inR.pdf" , width=8, height=5)

spacing <- c(0,.35,.35,.35,.35)
barplot(cps$Registered,names=cps$CPSmaxinter,  space=spacing, col="lightgray",main="Registration and Voting by Type of CPS Involvement", ylab="Percent Registered/Voting in 2016", cex.main=1.3, cex.lab=1.2, cex.names=.95, mgp = c(2.5, 0.5, 0))
barplot(cps$Voted,names=NULL, , col="gray30",  space= c(.2,.35,.35,.35,.35), mgp = c(2.5, 0.5, 0), add=T)
par(fig=c(0, 1, 0, 1), oma=c(0, 0, 0, 0), mar=c(0, 0, 0, 0), new=TRUE)
plot(0, 0, type='n', bty='n', xaxt='n', yaxt='n')
legend("bottom" , horiz=TRUE, inset=c(-0.1,0), legend=c("Registered", "Voted 2016"), fill=c("lightgray","gray30"))

dev.off()

